package homework.no_4;

import java.io.*;
import java.net.Socket;

public class TCPClient1 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        Socket socket = null;
        OutputStream os = null;
        InputStream is = null;


        try {
            fis = new FileInputStream("doc\\timg.gif");
            socket = new Socket("localhost", 8800);
            os = socket.getOutputStream();

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fis.read(bytes)) != -1) {

                os.write(bytes, 0, len);
            }
            socket.shutdownOutput();

            is = socket.getInputStream();


            while ((len = is.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, len));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }  catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != socket) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
